package cn.tedu.eb_film.service.impl;

import cn.tedu.eb_film.ex.ServiceException;
import cn.tedu.eb_film.mapper.SelectSeatMapper;
import cn.tedu.eb_film.pojo.dto.SelectSeatDTO;
import cn.tedu.eb_film.pojo.vo.*;
import cn.tedu.eb_film.service.*;
import cn.tedu.eb_film.utils.ServiceCode;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author ライナー・ロンテ
 * @since 2023/4/19 16:38
 */
@Slf4j
@Service
public class SelectSeatServiceImpl implements SelectSeatService {
    @Autowired
    private SelectSeatMapper selectSeatMapper;
    @Autowired
    private MovieService movieService;
    @Autowired
    private CinemaHallService cinemaHallService;
    @Autowired
    private UserService userService;
    @Autowired
    private CinemaScheduleService cinemaScheduleService;
    @Autowired
    private CinemaService cinemaService;


    @Override
    public SelectSeatVO loadSeat(SelectSeatDTO selectSeatDTO) {
        log.debug("开始进行场次页面数据加载{}",selectSeatDTO);
        final CinemaScheduleStandardVO schedule =
                cinemaScheduleService.getCinemaScheduleById(selectSeatDTO.getScheduleId());
        if (schedule == null) {
            String message = "加载失败,该场次不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }

        LocalDateTime movieStartTime = schedule.getMovieStartTime();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM月dd日  HH:mm");
        String format = movieStartTime.format(dateTimeFormatter);
        if (format.charAt(0) == '0'){
            format = format.substring(1);
        }

        final CinemaHallStandardVO hall =
                cinemaHallService.getStandardById(schedule.getHallId());
        if (hall == null) {
            String message = "加载失败,该放映厅不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }

        CinemaStandardVO cinema = cinemaService.getCinemaStandardVOById(hall.getCinemaId());
        if (cinema == null) {
            String message = "加载失败,该电影院不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }


        final MovieStandardVO movie = movieService.getStandardById(schedule.getMovieId());
        if (movie == null) {
            String message = "加载失败,该电影不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }

        String language = movie.getLanguageVersion();
        String detail = movie.getDimension();

        String languageVersion = language +"/"+ detail;

        Long selectSeatId = getSelectId(selectSeatDTO.getScheduleId());
        String map = getSeatMap(selectSeatId);
        Integer[][] integers = JSON.parseObject(map, Integer[][].class);


        final UserStandardVO user = userService.selectById(selectSeatDTO.getUserId());
        String phone = user.getPhone();
        String startPhone = phone.substring(0, 3);
        String endPhone = phone.substring(phone.length() - 4);
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(startPhone);
        stringBuffer.append("****");
        stringBuffer.append(endPhone);
        String ph = stringBuffer.toString();


        SelectSeatVO selectSeatVO = new SelectSeatVO();
        selectSeatVO.setDuration(movie.getDuration());
        selectSeatVO.setType(movie.getType());
        selectSeatVO.setLanguageVersion(languageVersion);
        selectSeatVO.setPhone(ph);
        selectSeatVO.setSchedule(format);
        selectSeatVO.setCinemaName(cinema.getName());
        selectSeatVO.setCinemaHallName(hall.getName());
        selectSeatVO.setSeat(integers);
        selectSeatVO.setMovieName(movie.getZnName());
        selectSeatVO.setMoviePictureUrl(movie.getPictureUrl());
        selectSeatVO.setMoviePrice(schedule.getMoviePrice());
        return selectSeatVO;
    }

    @Override
    public Long getSelectId(Long scheudleId) {
        return selectSeatMapper.getSeatId(scheudleId);
    }

    @Override
    public String getSeatMap(Long id) {
        return selectSeatMapper.getSeatMap(id);
    }
}
